package com.neusoft.neuiotms.dcom.service.change.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.change.ChangeCiRelationConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeCiRelationDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeCiRelation;
import com.neusoft.neuiotms.dcom.repository.change.ChangeCiRelationRepository;
import com.neusoft.neuiotms.dcom.service.change.ChangeCiRelationService;

@Service
public class ChangeCiRelationServiceImpl implements ChangeCiRelationService {

    @Autowired
    private ChangeCiRelationRepository changeCiRelationRepository;

    @Autowired
    private ChangeCiRelationConverter changeCiRelationConverter;

    @Override
    public List<ChangeCiRelationDTO> index(final Searchable searchable) {
        final List<ChangeCiRelation> models = this.changeCiRelationRepository.index(searchable);
        return this.changeCiRelationConverter.toListDTO(models);
    }

    @Override
    public Page<ChangeCiRelationDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<ChangeCiRelation> result = this.changeCiRelationRepository.search(pageable, searchable);
        return this.changeCiRelationConverter.toPageDTO(result);
    }

    @Override
    public ChangeCiRelationDTO create(final ChangeCiRelationDTO dto) {
        final List<ChangeCiRelation> relations = this.changeCiRelationRepository.findByRelationId(dto.getRelationId());
        if (CollectionUtils.isNotEmpty(relations)) {
            return this.changeCiRelationConverter.toDTO(relations.get(0));
        }
        final ChangeCiRelation model = this.changeCiRelationConverter.toModel(dto);
        this.changeCiRelationRepository.save(model);
        return this.changeCiRelationConverter.toDTO(model);
    }

    @Override
    public ChangeCiRelationDTO update(final Long id, final ChangeCiRelationDTO dto) {
        ChangeCiRelation model;
        try {
            model = this.changeCiRelationRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.changeCiRelationConverter.toModel(dto);
        this.changeCiRelationRepository.save(model);

        return this.changeCiRelationConverter.toDTO(model);
    }

    @Override
    public ChangeCiRelationDTO details(final Long id) {
        final ChangeCiRelation model;
        try {
            model = this.changeCiRelationRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.changeCiRelationConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        try {
            this.changeCiRelationRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        this.changeCiRelationRepository.deleteById(id);
    }

}
